package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.vo.DetailsVo;
import com.dongdongshop.pojo.vo.GoodsVo;
import com.dongdongshop.pojo.vo.ItemCatGoodsVo;
import com.dongdongshop.pojo.vo.TbGoodAllVo;
import com.dongdongshop.service.goods.GoodsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

@Service
@Component
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private TbGoodsMapper goodsMapper;

    @Resource
    private TbGoodsDescMapper goodsDescMapper;

    @Resource
    private TbItemMapper itemMapper;

    //mq
    @Resource
    private RocketMQTemplate rocketMQTemplate;


    /**
     * 保存商品信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGoods(TbGoods goods, TbGoodsDesc goodsDesc, List<TbItem> list) {
        //保存goods表并返回主键id
        goodsMapper.addGoods(goods);
        //保存goodsDes  goodsDes表的主键id不是自增的而是goods表的id值
        goodsDesc.setGoodsId(goods.getId());
        goodsDescMapper.insertSelective(goodsDesc);
        //保存item
        Stream<TbItem> stream = list.stream();
        Consumer<TbItem> consumer = i ->{
            i.setGoodsId(goods.getId());
            i.setUpdateTime(new Date());
        };
        stream.forEach(consumer);
        /*for (TbItem i : list) {
            i.setGoodsId(goods.getId());
            i.setUpdateTime(new Date());
        }*/
        itemMapper.addItemBatch(list);
    }

    @Override
    public List<ItemCatGoodsVo> findGoodsList(String auditStatus, String goodsName, String sellerId) {
        List<ItemCatGoodsVo> goods = goodsMapper.getGoodsList(auditStatus,goodsName,sellerId);
        return goods;
    }

    @Override
    public void updateByStatus(Long[] ids) {
        Stream<Long> stream = Stream.of(ids);
        Consumer<Long> consumer = x ->{
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(x);
            tbGoods.setAuditStatus("1");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
        /*for (int i = 0; i < ids.length; i++){
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(ids[i]);
            tbGoods.setAuditStatus("1");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        }*/
    }

    @Override
    public void deleteById(Long[] ids) {
        Stream<Long> stream = Stream.of(ids);
        Consumer<Long> consumer = x ->{
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(x);
            tbGoods.setIsDelete("2");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
        /*for (int i = 0;i <ids.length ;i++){
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(ids[i]);
            tbGoods.setIsDelete("2");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        }*/
    }

    /**
     * 根据商品id查询商品
     */
    @Override
    public TbGoods finGoodsById(Long goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public List<GoodsVo> getGoodsShInfo(String goodsName) {
        List<GoodsVo> goodsVos = goodsMapper.selectByGoodsShVo(goodsName);
        return goodsVos;
    }

    @Override
    public void deleteBatch(Long[] ids) {
        Stream<Long> ids1 = Stream.of(ids);
        Consumer<Long> consumer = x ->{
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(x);
            tbGoods.setIsDelete("2");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        ids1.forEach(consumer);
        /*for (int i = 0;i <ids.length ;i++){
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(ids[i]);
            tbGoods.setIsDelete("2");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        }*/
    }



    @Override
    public List<DetailsVo> getDetailsById(Long id) {
        List<DetailsVo> detailsVos = goodsMapper.getDetailsById(id);
        return detailsVos;
    }

    //修改状态为
    @Override
    public void updateByAudit(Long[] ids) {


        Stream<Long> stream = Stream.of(ids);
        Consumer<Long> consumer = x ->{
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(x);
            tbGoods.setAuditStatus("2");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
//        for (int i = 0; i < ids.length; i++){
//            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(ids[i]);
//            tbGoods.setAuditStatus("2");
//            goodsMapper.updateByPrimaryKeySelective(tbGoods);
//        }

        for (Long id :ids){
            rocketMQTemplate.convertAndSend("staticProducer",id);
        }
    }

    //修改状态为
    @Override
    public void updateByAuditStatus(Long[] ids) {

        Stream<Long> stream = Stream.of(ids);
        Consumer<Long> consumer = x ->{
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(x);
            tbGoods.setAuditStatus("3");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
//        for (int i = 0; i < ids.length; i++){
//            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(ids[i]);
//            tbGoods.setAuditStatus("3");
//            goodsMapper.updateByPrimaryKeySelective(tbGoods);
//        }
    }

    @Override
    public TbGoodAllVo getGoodsById(Long o) {
        return goodsMapper.getGoodsById(o);
    }
}
